package com.threeti.mecool.web.util;

import org.codehaus.jackson.map.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public final class WebResponse {

	public static final String GLOBAL_SUBMIT_CALLBACK_PAGE="/includeSubmitCallBack";

	private static WebResponse instance;

	private boolean success=true;

	private String msgBody;

	private Map<String,Object> extraData=new HashMap<String, Object>();

	private WebResponse(){
	}

	public boolean isSuccess() {
		return success;
	}

	public WebResponse setSuccess(boolean success) {
		this.success = success;
		return instance;
	}

	public String getMsgBody() {
		return msgBody;
	}

	public WebResponse setMsgBody(String msgBody) {
		this.msgBody = msgBody;
		return instance;
	}

	public WebResponse setExceptionMsg(Throwable throwable) {
		this.msgBody = throwable.getMessage();
		this.success=false;
		return instance;
	}

	public Map<String, Object> getExtraData() {
		return extraData;
	}

	public WebResponse putExtraData(String key,Object value) {
		extraData.put(key, value);
		return instance;
	}

	public static WebResponse newInstance(){
		instance=new WebResponse();
		return instance;
	}

	public void buildModelMapData(Map modelMap){
		if(instance==null){
			return;
		}
		ObjectMapper mapper = new ObjectMapper();
		try {
			modelMap.put("responseData", mapper.writeValueAsString(this));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
